package test;

import java.net.UnknownHostException;

import system.Config;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class GenUser {

	/**
	 * @param args
	 * @throws MongoException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException, MongoException {
		Mongo m = new Mongo();
		DB db = m.getDB("game");
		DB log = m.getDB("log");
		DBCollection players = db.getCollection("player");
		players.ensureIndex("name");
		players.ensureIndex("guid");		
		DBCollection records = log.getCollection("record");
		records.ensureIndex("name");
		records.ensureIndex("guid");
		BasicDBObject record;
		BasicDBObject player;
		BasicDBObject pet;
		int[] skills = {1, 2, 3};
		pet = new BasicDBObject();
		pet.put("nature", 1);
		pet.put("profession", 1);
		pet.put("name", "pet1");
		pet.put("level", 1);
		pet.put("currentSkill", skills);
		pet.put("grow", 1);
		pet.put("figureID", 1);
		BasicDBObject[] equips = new BasicDBObject[3];
		for (int i = 0; i < equips.length; i++) {
			equips[i] = new BasicDBObject();
			equips[i].put("ID", 1);
			equips[i].put("level", 1);			
		}
		pet.put("currentEquips", equips);
		BasicDBObject[] currentElements = new BasicDBObject[3];
		for (int i = 0; i < currentElements.length; i++) {
			currentElements[i] = new BasicDBObject();
			currentElements[i].put("ID", 1);
			currentElements[i].put("quality", 1);
			currentElements[i].put("level", 1);
		}
		pet.put("currentElements", currentElements);
		pet.put("quality", 1);
		BasicDBObject[] pets = {pet, pet, pet};
		String rname;
		long now = System.currentTimeMillis();
		
		int levelNum = 3000;
		int rid = 0;
		int range;
		int score;
		
		for (int i = 0;i < Config.gradeScore.length; i++) {
			for (int j = 0; j < levelNum; j++) {
				record = new BasicDBObject();
				player = new BasicDBObject();
				rid = i * levelNum + j;
				rname = "Robot" + String.format("%06d", rid);
//				rname = "robot" + String.format("%02d", i)  + "_" + String.format("%04d", j);
//				rname = "robot" + i + "_" + j;
				record.put("name", rname);
				record.put("level", i+1);
				record.put("guid", rname);
				record.put("whetherSkillUnlock", 1);
				record.put("currentPets", pets);
				
				player.put("guid", rname);
				player.put("name", rname);
				player.put("level", i+1);
				player.put("sex", 1);
				player.put("pvpWinCount", 0);
				player.put("pvpCount", 0);
				player.put("lastEnterPVPTime", now);
				player.put("lastActiveTime", now);
//				player.put("lastEnterPVPTime", 0);

				if (i != Config.gradeScore.length-1) {
					range = Config.gradeScore[i+1]-Config.gradeScore[i];
				} else {
					range = 100;
				}
				score = Config.gradeScore[i]+(int)(range*j/levelNum);
				
				player.put("pvpScore", score);
				
				records.insert(record);
				players.insert(player);
				
				System.out.println("insert " + record.getString("name") + " score:(" + score + ")");
			}
		}
	}

}
